package online.sh.cn.fy.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ContentHandler;
import java.net.ContentHandlerFactory;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

public class WxUtils {
	public static String getSystime(){
    	String time = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    	return time;
    }
	
	//获取业务接口token
	public String getshToken(){
		try {
			String url="http://m.sh.189.cn/service/1.0/access/token?appid=ctfeiyoung&secret=9c72593f7a72df73fc73b80d6d8268";
			URL getUrl = new URL(url); 
			HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
			connection.connect(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
	        String lines=null; 
	        String s=null;
	        while ((lines = reader.readLine()) != null) { 
	               s=lines; 
	        } 
	        reader.close(); 
	        System.out.println(connection.getContentType());
	        connection.disconnect(); 
	        return s;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}			
	
	//获取认证url
	public String getshURL(String token,String checkurl){
		try {
			String url="http://m.sh.189.cn/service/wxoauth/2.0/getBaseUrl?access_token="+token;
			String state="state="+URLEncoder.encode(checkurl);
			URL getUrl = new URL(url); 
			HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
//			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencode; charset=utf-8");
			connection.connect();
//			Map< String,List< String>> map = connection.getHeaderFields();
//			//遍历所有的响应头字段
//			for (String key : map.keySet()){
//				System.out.println(key + "--->" + map.get(key));
//			}
//			Map<String,String> map=new HashMap<String,String>();
//			map.put("content", Mesg);
//			map.put("msgtype", "text");
//			map.put("touser", openid);
//			String ask=JSONObject.fromObject(map).toString();
//			System.out.println(ask);
//			PrintWriter out=new PrintWriter(connection.getOutputStream());
//			out.println(ask);
//			out.flush();
			OutputStream out=connection.getOutputStream();
			out.write(state.getBytes());
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
	        String lines=null;
	        String s=null;
	        while ((lines = reader.readLine()) != null) { 
	               s=lines; 
	        } 
	        reader.close(); 
	        connection.disconnect();
	        return s;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}					
			
	//获取认证openid
	public String getshOpenid(String token,String code){
		try {
			String url="http://m.sh.189.cn/service/wxoauth/2.0/getOpenidByCode?access_token="+token;
			String state="code="+code;
			URL getUrl = new URL(url); 
			HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.connect();
			OutputStream out=connection.getOutputStream();
			out.write(state.getBytes());
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
	        String lines=null;
	        String s=null;
	        while ((lines = reader.readLine()) != null) { 
	               s=lines; 
	        } 
	        reader.close(); 
	        connection.disconnect();
	        return s;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}		
			
	//向网厅后台发送手机
	public String sendMesg(String token,String openid,String deviceno){
		try {
			String url="http://m.sh.189.cn/service/1.0/complexBind?access_token="+token;
//			String jsonpost="{\"openid\":\""+openid+"\",\"deviceno\":\""+deviceno+"\",\"from\":\"feiYoung\"}";
			String socket="openid="+openid+"&deviceno="+deviceno+"&from=feiYoung";
			URL getUrl = new URL(url); 
			HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.connect();
//			Map<String,String> map=new HashMap<String,String>();
//			map.put("content", Mesg);
//			map.put("msgtype", "text");
//			map.put("touser", openid);
//			String ask=JSONObject.fromObject(map).toString();
//			System.out.println(ask);
//			PrintWriter out=new PrintWriter(connection.getOutputStream());
//			out.println(ask);
//			out.flush();
			OutputStream out=connection.getOutputStream();
			out.write(socket.getBytes());
	        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
	        String lines=null;
	        String s=null;
	        while ((lines = reader.readLine()) != null) { 
	               s=lines; 
	        } 
	        reader.close(); 
	        connection.disconnect();
	        return s;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}			
			
	//获取openid
		public String getOpenidAndToken(String code){
			try {
				String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxe643cc1b34aa8f25&secret=90ebfc583953b6cff690fc25eff11e91&code="+code+"&grant_type=authorization_code";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}	
		
	//获取accesstoken
		public String getAccessToken(){
			try {
				String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe643cc1b34aa8f25&secret=90ebfc583953b6cff690fc25eff11e91";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}			
		
	//调用用户信息
		public String getUserinfo(String accesstoken,String openid){
			try {
				String url="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accesstoken+"&openid="+openid+"&lang=zh_CN";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
	//调用用户手机信息
		public JSONObject getMoblieinfo(String mobile){
			try {
				String url="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+mobile;
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"gb2312")); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=s+lines; 
		        } 
		        reader.close(); 
		        connection.disconnect();
		        s=s.replace("null__GetZoneResult_ = ", "");
		        JSONObject json=JSONObject.fromObject(s);
		        return json;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}		
		
		public static void main(String[] args) {
			WxUtils wx=new WxUtils();
			System.out.println(wx.getshToken());
//			System.out.println(wx.getshURL("EdaJes0p0=dE4gIIjXaQGudHQaZDdvYHqTE=Kxf2U-bV6dm6l=muH9VFY8S0s", "http://www.baidu.com"));
//			System.out.println(wx.sendMesg("RsOm5ISGkg7jfm-LX-nQ2JlA=ksufgdXRoQT847MEnkeEjwB1i8nmL9T=d1pn", "oKXUCjwytCa6TeqFWWzomQXxyFsA", "18918701127"));
//			try {
//				Date date=new Date();
//				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
//				Date old = format.parse("2017-04-06");
//				System.out.println((date.getTime()-old.getTime())/(1000*60*60*24)>15?"大于15":"小于15");
//			} catch (ParseException e) {
//				
//			}
		}
}
